package com.android.juguotang.fragment.member;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.juguotang.model.recchagepostCallback;
import com.android.juguotang.model.recchagepostdata;
import com.android.juguotang.utils.LoginState;
import com.zhy.http.okhttp.callback.Callback;
import com.android.juguotang.R;
import com.android.juguotang.fragment.BaseLazyFragment;
import com.android.juguotang.model.MainTabData;
import com.android.juguotang.model.accountsOnlineCallback;
import com.android.juguotang.model.accountsOnlinedata;
import com.android.juguotang.model.ajaxcartCallback;
import com.android.juguotang.model.ajaxcartdata;
import com.android.juguotang.utils.ConstantsUrl;
import com.android.juguotang.utils.HttpUtils;
import com.android.juguotang.utils.MyLog;
import com.google.gson.Gson;
import com.mcxtzhang.nestlistview.NestFullListView;
import com.mcxtzhang.nestlistview.NestFullListViewAdapter;
import com.mcxtzhang.nestlistview.NestFullViewHolder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class Fragment_Mine_Accounts_Online extends BaseLazyFragment {
    @BindView(R.id.refill_style_list)
    NestFullListView refill_style_list;
    @BindView(R.id.refill_pay_style_list)
    NestFullListView refill_pay_style_list;
    @BindView(R.id.tv_accounts_online_refill)
    TextView tv_accounts_online_refill;


    private FragmentActivity mcontext;
    private View view;

    public Fragment_Mine_Accounts_Online() {

    }
    public static Fragment_Mine_Accounts_Online newInstance(String price) {
        Fragment_Mine_Accounts_Online fragment = new Fragment_Mine_Accounts_Online();
        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, price);
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private boolean isCheck = true;

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }
    private String[] pay_ids = new String[2];
    private String pay_id = "42";
    private String amount="";
    private ArrayList<MainTabData> mSortListdata = new ArrayList<>();
    ArrayList<HashMap<String,String>> discountList=new ArrayList<>();
    ArrayList<String> discountListKeys=new ArrayList<>();
    public int mCurrentitem=0;
    private int pospay = 0;
    @Override
    protected void initData() {
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERRECCHAGE;
        HttpUtils.get(address, null, null, new Callback() {

            @Override
            public accountsOnlinedata parseNetworkResponse(Response response, int id) throws Exception {
                String string = response.body().string();
                JSONTokener jsonParser = new JSONTokener(string);
                // 此时还未读取任何json文本，直接读取就是一个JSONObject对象。
                // 如果此时的读取位置在"name" : 了，那么nextValue就是"yuanzhifei89"（String）
                JSONObject person = (JSONObject) jsonParser.nextValue();
                // 接下来的就是JSON对象的操作了
                String msg = person.getString("msg");
                JSONObject data = person.getJSONObject("data");
                JSONObject discount = data.getJSONObject("discount");
                Iterator<String> keys = discount.keys();


                while(keys.hasNext()){
                   String next = keys.next();
                   String value = discount.getString(next);
//                   MyLog.e("inest","next="+next+"       "+value);
                   HashMap<String,String> hash=new HashMap<>();
                   hash.put(next,value);
                   discountList.add(hash);
                    discountListKeys.add(next);
               }

//                MyLog.e("inest","msg="+msg+"");
                return null;
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }


            @Override
            public void onResponse(Object response, int id) {

//                discountList.add("1");discountList.add("1");discountList.add("1");discountList.add("1");
//MyLog.e("inest","discountList="+response.b);
                Gson gson=new Gson();
//                String json=gson.toJson(response.getData());

refill_style_list.setAdapter(new NestFullListViewAdapter(R.layout.item_accounts_online,discountList) {
                    @Override
                    public void onBind(final int i, Object o, NestFullViewHolder nestFullViewHolder) {
                        nestFullViewHolder.setText(R.id.tv_account_online_refill,"充"+discountListKeys.get(i));

                        nestFullViewHolder.setText(R.id.tv_account_online_send,"送"+discountList.get(i).get(discountListKeys.get(i)));

                        nestFullViewHolder.getView(R.id.item_account_online_check).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                amount=discountListKeys.get(i);
                                mCurrentitem=i;
                                refill_style_list.setAdapter(new NestFullListViewAdapter(R.layout.item_accounts_online,discountList) {
                                    @Override
                                    public void onBind(int i, Object o, NestFullViewHolder nestFullViewHolder) {
                                        nestFullViewHolder.setText(R.id.tv_account_online_refill,"充"+discountListKeys.get(i));
                                        nestFullViewHolder.setText(R.id.tv_account_online_send,"送"+discountList.get(i).get(discountListKeys.get(i)));
                                        if (i == mCurrentitem) {//最后选择的位置
                                            nestFullViewHolder.setChecked(R.id.item_account_online_check, true);
                                        } else {
                                            nestFullViewHolder.setChecked(R.id.item_account_online_check, false);
                                        }
                                    }
                                });
                            }
                        });

                    }
                });
                refill_style_list.setOnItemClickListener(new NestFullListView.OnItemClickListener() {

                    @Override
                    public void onItemClick(NestFullListView nestFullListView, View view, int i) {

                    }
                });
                mSortListdata.add(new MainTabData("微信支付","42",R.mipmap.support_pay_wx));
               // mSortListdata.add(new MainTabData("支付宝支付","41",R.mipmap.support_pay_wx));

                refill_pay_style_list.setAdapter(new NestFullListViewAdapter<MainTabData>(R.layout.item_pay, mSortListdata) {
                    @Override
                    public void onBind(int position, MainTabData testBean, NestFullViewHolder holder) {
                        holder.setText(R.id.pay_name, testBean.getName());
                        holder.setImageResource(R.id.pay_logo, testBean.getThumb());
                        pay_ids[position] = testBean.getId() + "";
                        if (pospay == position) {//最后选择的位置
                            holder.setChecked(R.id.item_list_mine_pay_check, true);
                        } else {
                            holder.setChecked(R.id.item_list_mine_pay_check, false);
                        }
                    }
                });
                refill_pay_style_list.setOnItemClickListener(new NestFullListView.OnItemClickListener() {
                    @Override
                    public void onItemClick(NestFullListView nestFullListView, View view, int i) {
                        pay_id = mSortListdata.get(i).getId();
                        pospay = i;
                        refill_pay_style_list.setAdapter(new NestFullListViewAdapter<MainTabData>(R.layout.item_pay, mSortListdata) {
                            @Override
                            public void onBind(int position, MainTabData testBean, NestFullViewHolder holder) {
                                holder.setText(R.id.pay_name, testBean.getName());
                                holder.setImageResource(R.id.pay_logo, testBean.getThumb());

                                if (pospay == position) {//最后选择的位置
                                    holder.setChecked(R.id.item_list_mine_pay_check, true);
                                } else {
                                    holder.setChecked(R.id.item_list_mine_pay_check, false);
                                }
                            }
                        });
                    }
                });
            }
        });
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine_accounts_online, container, false);
        ButterKnife.bind(this, view);
        mcontext = getActivity();
        tv_accounts_online_refill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String address = ConstantsUrl.domain + "member/recchage";
                HashMap<String, String> map = new HashMap<>();
                map.put("pay_id",pay_id);
                map.put("amount",amount);
                map.put("Submit","充值");
                HttpUtils.post(address, map, new recchagepostCallback() {


                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(recchagepostdata response, int id) {
                        if(response.getFlag()){


                        String order_sn=response.getData().getOrder().getOrder_sn();
                        String login_id=response.getData().getOrder().getLog_id();
                        int order_amount=response.getData().getOrder().getOrder_amount();
//                        String pay_fee=response.getData().getOrder().get;
                        String out_trade_no=response.getData().getOrder().getOut_trade_no();
                        pay(order_sn,login_id,order_amount,out_trade_no);
                        }
                    }


                });
            }
        });

        return view;
    }

    private void pay(String order_sn,String login_id,int order_amount,String out_trade_no) {
        String address = ConstantsUrl.domain +  "order/pay";
        String getid = LoginState.getInstance().getid(mcontext);
        String getpd = LoginState.getInstance().getpd(mcontext);
        HashMap<String, String> header = new HashMap<>();
        header.put("UID", getid);
        header.put("UPSW", getpd);
        HashMap<String, String> map = new HashMap<>();
//        map.put("login_id",login_id);
//        map.put("order_amount",""+order_amount);

        map.put("order_sn",order_sn);
        map.put("out_trade_no",out_trade_no);


        HttpUtils.get(address,header, map, new ajaxcartCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(ajaxcartdata response, int id) {

            }
        });
    }




}
